﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DARE
{
    public class CBoundingCircle: ABoundingElement
    {
        #region fields

        private float m_ray;
        private Vector2 m_center;

        #endregion

        #region properties

        public float Ray { get { return m_ray; } set { m_ray = value; } }
        public Vector2 Center { get { return m_center; } set { m_center = value; } }

        #endregion

        #region ctor

        public CBoundingCircle(string name, CRender2D render2D)
            : this(name, render2D, null)
        {
        }

        public CBoundingCircle(string name, CRender2D render2D, AEntity parent)
            : base(name, render2D, parent)
        {

        }


        #endregion

        #region inherited

        public override bool Contains(CBoundingCircle element)
        {
            float lenght = (element.Center - m_center).Length();
            if (lenght + element.Ray <= m_ray)
                return true;
            return false;
        }

        public override bool Contains(CBoundingRectangle element)
        {
            Vector2 topleft = new Vector2(element.Rectangle.Left, element.Rectangle.Top);
            Vector2 topright = new Vector2(element.Rectangle.Right, element.Rectangle.Top);
            Vector2 bottomleft = new Vector2(element.Rectangle.Left, element.Rectangle.Bottom);
            Vector2 bottomright = new Vector2(element.Rectangle.Right, element.Rectangle.Bottom);
            if ((topleft - m_center).Length() > m_ray)
                return false;
            if ((topright - m_center).Length() > m_ray)
                return false;
            if ((bottomleft - m_center).Length() > m_ray)
                return false;
            if ((bottomright - m_center).Length() > m_ray)
                return false;
            return true;
        }

        public override bool Intersects(CBoundingCircle element)
        {
            float lenght = (element.Center - m_center).Length();
            if (lenght <= m_ray + element.Ray)
                return true;
            return false;
        }

        public override bool Intersects(CBoundingRectangle element)
        {
            return element.Intersects(this);
        }

        #endregion

        #region methods

        public override AEntity Clone(string name)
        {
            return (Clone(name, null));
        }

        public override AEntity Clone(string name, AEntity parent)
        {
            CBoundingCircle circle = new CBoundingCircle(name, m_render);
            circle.Parent = parent;
            circle.m_children = new Dictionary<string, AEntity>();
            foreach (AEntity child in m_children.Values)
                circle.m_children.Add(child.Name, child.Clone(child.Name, circle));
            circle.Position = m_position;
            return circle;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            throw new NotImplementedException();
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            throw new NotImplementedException();
        }

        #endregion

       

    }
}
